﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Assistant.Entities;
using Microsoft.Xna.Framework;

/**
 * 创建时间：2014/11/3 14:17:52
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace Assistant
{
    public class AppHelper
    {
        #region ******************************AssemblyInfo******************************
        public static Version GetAssemblyVersion
        {
            get
            {
                return new AssemblyName(Assembly.GetExecutingAssembly().FullName).Version;
            }
        }


        public static string GetAssemblyTitle
        {
            get
            {
                return ((AssemblyTitleAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false)[0]).Title;
            }
        }

        public static string GetAssemblyDescription
        {
            get
            {
                return ((AssemblyDescriptionAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false)[0]).Description;
            }
        }


        public static string GetAssemblyCompany
        {
            get
            {
                return ((AssemblyCompanyAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false)[0]).Company;
            }
        }


        public static string GetAssemblyProduct
        {
            get
            {
                return ((AssemblyProductAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0]).Product;
            }
        }

        public static string GetAssemblyCopyright
        {
            get
            {
                return ((AssemblyCopyrightAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0]).Copyright;
            }
        }
        #endregion ******************************AssemblyInfo******************************


        #region ******************************WMAppManifest******************************
        /// <summary>
        /// 获取WMAppManifest.xml中App的基本信息
        /// </summary>
        /// <returns></returns>
        public static WMAppManifest GetWMAppManifestInfo
        {
            get
            {
                WMAppManifest result = new WMAppManifest();
                try
                {
                    using (Stream stream = TitleContainer.OpenStream("WMAppManifest.xml"))
                    {
                        XElement xElement = XElement.Load(stream);
                        IEnumerable<XElement> appElements = xElement.Descendants("App");
                        //
                        result.ApplicationProductId = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("ProductID").Value));
                        result.ApplicationTitle = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Title").Value));
                        result.ApplicationVersion = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Version").Value));
                        result.ApplicationAuthor = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Author").Value));
                        result.ApplicationDescription = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Description").Value));
                        result.ApplicationPublisher = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Publisher").Value));
                        result.ApplicationPublisherId = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("PublisherID").Value));
                        //
                        result.ApplicationProductId = result.ApplicationProductId.Replace("{", "").Replace("}", "");
                        result.ApplicationPublisherId = result.ApplicationPublisherId.Replace("{", "").Replace("}", "");
                        //
                        stream.Close();
                        stream.Dispose();
                    }
                }
                catch (Exception err)
                {
                    LogHelper.LogError(err.ToString(), typeof(AppHelper), MethodBase.GetCurrentMethod());
                }

                return result;
            }
        }
        #endregion ******************************WMAppManifest******************************
    }
}
